package user;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import user.User;
import user.UserDao;

/**
 * @author Administrator
 */
public class UserDaoMap implements UserDao {
  HashMap<String,User> map=new HashMap<>();
    @Override
    public void register(User user) {
      map.put(user.getName(),user);
    }

    @Override
    public boolean login(String name, String password) {
      Iterator iterator=map.keySet().iterator();
      while (iterator.hasNext()){
        // 获取key和value
        String key= (String) iterator.next();
        User value=map.get(key);
        // 比较
        if(name.equals(key)&&password.equals(value.getPassword())){
          return true;
        }
      }
        return false;
    }

    @Override
    public void queryAll() {
      System.out.println("所有用户信息如下：");
      Iterator iterator=map.entrySet().iterator();
      while (iterator.hasNext()){
        Map.Entry<String,User> entry= (Map.Entry<String, User>) iterator.next();
        //  String key=entry.getKey();
        User value=entry.getValue();
        System.out.println(value);
      }
    }
}
